/*
 * jfCommunicationSettings.java
 *
 * Created on 11 mai 2008, 16:43
 */
package bbos.Match.Automat.BeforeMatch;

import bbos.General.Views.*;
import bbos.*;
import bbos.General.Model.mPlayerType;
import bbos.General.Model.mPlayer;
import bbos.General.Model.mTeamRoster;
import bbos.General.Views.Rosters.jtcRenderer;
import bbos.Match.Model.Competences.dCompetenceType;
import bbos.Match.Model.Competences.dCompetencesFactory;
import bbos.Match.Model.dPlayer;
import bbos.Match.Model.dTeam;
import bbos.Match.Model.rmiPlayer;
import bbos.Match.Model.rmiTeam;
import javax.swing.*;
import java.awt.*;
import java.rmi.RemoteException;
import java.util.Vector;
import javax.swing.table.TableColumn;

/**
 *
 * @author  frederic
 */
public class jdgChooseMercenary extends JDialog {

    long _money;
    rmiTeam _model;
    Vector _mercenaries;
    Vector _availableMercenaries;
    boolean _isChallenger;

    /** Creates new form jfCommunicationSettings */
    public jdgChooseMercenary(rmiTeam model, long money, Vector mercenaries, boolean isChallenger) {
        initComponents();

        _isChallenger = isChallenger;
        GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
        GraphicsDevice gs = ge.getDefaultScreenDevice();
        DisplayMode dmode = gs.getDisplayMode();

        if (dmode != null) {
            int screenWidth = dmode.getWidth();
            int screenHeight = dmode.getHeight();
            this.setLocation((screenWidth - this.getWidth()) / 2, (screenHeight - this.getHeight()) / 2);
        }

        _money = money;
        _model = model;
        _mercenaries = mercenaries;

        _availableMercenaries = new Vector();
        try {
            mTeamRoster roster = mBBoS.getSingleton().getTeamType(_model.getRaceId());

            for (int i = 0; i < roster.getRegularPlayersNumber(); i++) {
                mPlayerType player = roster.getPlayerType(i);
                int limit = player.getLimit();
                int nb=_model.getPlayersNumber();
                
                for (int j = 0; j < nb; j++) {
                    dPlayer p = _model.getPlayerCopy(j);
                    if (p.getPosition().equals(player.getPosition())) {
                        limit--;
                    }
                }
                
                for (int j = 0; j < _mercenaries.size(); j++) {
                    dPlayer p = (dPlayer) _mercenaries.get(j);
                    if (p.getPosition().equals(player.getPosition())) {
                        limit--;
                    }
                }
                
                if (((player.getCost() + 30000) <= _money) && (limit > 0)) {
                    _availableMercenaries.add(player);
                }
            }
        } catch (RemoteException e) {
            System.err.println(e.getMessage());
        }

        jtmAvailableMercenary tableModel = new jtmAvailableMercenary(_availableMercenaries);

        jtbMercenaries.setModel(tableModel);
        jtbMercenaries.setDefaultRenderer(String.class, new jtcRenderer());
        jtbMercenaries.setDefaultRenderer(Integer.class, new jtcRenderer());

        TableColumn column = null;
        for (int i = 0; i < jtbMercenaries.getModel().getColumnCount(); i++) {
            column = jtbMercenaries.getColumnModel().getColumn(i);
            switch (i) {
                case 0:
                    column.setPreferredWidth(100);
                    break;
                case 1:
                    column.setPreferredWidth(15);
                    break;
                case 2:
                    column.setPreferredWidth(15);
                    break;
                case 3:
                    column.setPreferredWidth(15);
                    break;
                case 4:
                    column.setPreferredWidth(15);
                    break;
                case 5:
                    column.setPreferredWidth(450);
                    break;
                case 6:
                    column.setPreferredWidth(40);
                    break;
            }
        }

        repaint();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jtbMercenaries = new javax.swing.JTable();
        jPanel2 = new javax.swing.JPanel();
        jbtCancel = new javax.swing.JButton();
        jbtOK = new javax.swing.JButton();
        jPanel3 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DO_NOTHING_ON_CLOSE);
        setTitle("Treasury to Petty Cash");
        setMinimumSize(new java.awt.Dimension(200, 100));
        setModal(true);
        setResizable(false);

        jPanel1.setPreferredSize(new java.awt.Dimension(800, 200));
        jPanel1.setLayout(new java.awt.BorderLayout());

        jtbMercenaries.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        jScrollPane1.setViewportView(jtbMercenaries);

        jPanel1.add(jScrollPane1, java.awt.BorderLayout.CENTER);

        getContentPane().add(jPanel1, java.awt.BorderLayout.CENTER);

        jPanel2.setPreferredSize(new java.awt.Dimension(800, 33));

        jbtCancel.setText("Cancel");
        jbtCancel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtCancelActionPerformed(evt);
            }
        });
        jPanel2.add(jbtCancel);

        jbtOK.setText("OK");
        jbtOK.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtOKActionPerformed(evt);
            }
        });
        jPanel2.add(jbtOK);

        getContentPane().add(jPanel2, java.awt.BorderLayout.SOUTH);

        jPanel3.setPreferredSize(new java.awt.Dimension(800, 33));

        jLabel1.setText("Select a mercenary :");
        jPanel3.add(jLabel1);

        getContentPane().add(jPanel3, java.awt.BorderLayout.NORTH);

        pack();
    }// </editor-fold>//GEN-END:initComponents
    private void jbtCancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtCancelActionPerformed

        this.setVisible(false);
}//GEN-LAST:event_jbtCancelActionPerformed

    private void jbtOKActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtOKActionPerformed
        int index = jtbMercenaries.getSelectedRow();
        if (index > -1) {
            mPlayerType playerType = (mPlayerType) _availableMercenaries.get(index);
            try {
                dPlayer player = new dPlayer(playerType, _isChallenger, _model.getRace(),null);                
                player.setCost(player.getCost() + 30000);
                player.addCompetence(dCompetencesFactory.createCompetence("Loner"));
                
                if ((_money - player.getCost()) >= 50000) {
                    Vector competences = new Vector();
                    competences.add("None");
                    Vector normal = playerType.getSimpleRoll();
                    for (int i = 0; i < normal.size(); i++) {
                        dCompetenceType type = (dCompetenceType) normal.get(i);
                        competences.addAll(type.getCompetenceStringList());
                    }

                    String choice[] = new String[competences.size()];
                    for (int i = 0; i < competences.size(); i++) {
                        choice[i] = (String) competences.get(i);
                    }
                    Object response = JOptionPane.showInputDialog(
                            null,
                            "Do you want to add a competence to your player for 50 000 gc ?",
                            "Select a Competence",
                            JOptionPane.QUESTION_MESSAGE,
                            null,
                            choice, choice[0]);

                    if (!response.equals("None")) {
                        player.setCost(player.getCost() + 50000);
                        player.addCompetence(dCompetencesFactory.createCompetence((String) response));
                    }

                }
                player.setName("Mercenary " + Integer.toString(_mercenaries.size() + 1));

                _mercenaries.add(player);
            } catch (RemoteException e) {
                e.printStackTrace();
            }
            this.setVisible(false);
        }
}//GEN-LAST:event_jbtOKActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel jLabel1;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JButton jbtCancel;
    private javax.swing.JButton jbtOK;
    private javax.swing.JTable jtbMercenaries;
    // End of variables declaration//GEN-END:variables
    @Override
    public void paint(Graphics g) {
        super.paint(g);

    }
}

